package com.ruoyi.system.service.estate.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.estate.EstateResidentMapper;
import com.ruoyi.system.domain.estate.Resident;
import com.ruoyi.system.service.estate.IResidentService;
import com.ruoyi.system.mapper.SysUserResidentMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;

/**
 * 住户管理Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class ResidentServiceImpl implements IResidentService 
{
    @Autowired
    private EstateResidentMapper residentMapper;

    @Autowired
    private SysUserResidentMapper userResidentMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询住户信息
     * 
     * @param residentId 住户ID
     * @return 住户信息
     */
    @Override
    public Resident selectResidentById(Long residentId)
    {
        return residentMapper.selectResidentById(residentId);
    }

    /**
     * 查询住户列表
     * 
     * @param resident 住户信息
     * @return 住户
     */
    @Override
    public List<Resident> selectResidentList(Resident resident)
    {
        return residentMapper.selectResidentList(resident);
    }

    /**
     * 新增住户
     * 
     * @param resident 住户信息
     * @return 结果
     */
    @Override
    public int insertResident(Resident resident)
    {
        resident.setDelFlag("0");
        return residentMapper.insertResident(resident);
    }

    /**
     * 修改住户
     * 
     * @param resident 住户信息
     * @return 结果
     */
    @Override
    public int updateResident(Resident resident)
    {
        return residentMapper.updateResident(resident);
    }

    /**
     * 批量删除住户
     * 
     * @param residentIds 需要删除的住户ID
     * @return 结果
     */
    @Override
    public int deleteResidentByIds(Long[] residentIds)
    {
        return residentMapper.deleteResidentByIds(residentIds);
    }

    /**
     * 删除住户信息
     * 
     * @param residentId 住户ID
     * @return 结果
     */
    @Override
    public int deleteResidentById(Long residentId)
    {
        return residentMapper.deleteResidentById(residentId);
    }

    /**
     * 关联住户和用户
     * 
     * @param residentId 住户ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int linkResidentUser(Long residentId, Long userId) {
        // 先检查住户和用户是否存在
        Resident resident = selectResidentById(residentId);
        if (resident == null) {
            throw new ServiceException("住户不存在");
        }
        
        // 检查用户是否存在
        SysUser user = userService.selectUserById(userId);
        if (user == null) {
            throw new ServiceException("用户不存在");
        }
        
        // 先解除原有关联
        userResidentMapper.deleteUserResidentByResidentId(residentId);
        
        // 更新 t_resident 表中的 user_id 字段
        resident.setUserId(userId);
        residentMapper.updateResident(resident);
        
        // 建立新关联
        return userResidentMapper.insertUserResident(userId, residentId, SecurityUtils.getUsername());
    }

    /**
     * 解除住户和用户的关联
     * 
     * @param residentId 住户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int unlinkResidentUser(Long residentId) {
        // 查询住户信息
        Resident resident = selectResidentById(residentId);
        if (resident != null) {
            // 清除住户表中的 user_id 字段
            resident.setUserId(null);
            residentMapper.updateResident(resident);
        }
        
        // 删除关联表中的记录
        return userResidentMapper.deleteUserResidentByResidentId(residentId);
    }

    /**
     * 创建住户并关联用户
     * 
     * @param resident 住户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertResidentAndLinkUser(Resident resident) {
        // 新增住户信息
        int rows = insertResident(resident);
        
        // 关联用户
        if (rows > 0 && resident.getUserId() != null) {
            userResidentMapper.insertUserResident(resident.getUserId(), resident.getResidentId(), SecurityUtils.getUsername());
        }
        
        return rows;
    }

    /**
     * 根据用户ID查询住户
     * 
     * @param userId 用户ID
     * @return 住户信息
     */
    @Override
    public Resident selectResidentByUserId(Long userId) {
        // 首先尝试从关联表中查询
        Resident resident = userResidentMapper.selectResidentByUserId(userId);
        
        // 如果关联表中没有找到，则尝试从 t_resident 表中直接查询
        if (resident == null) {
            resident = residentMapper.selectResidentByUserId(userId);
        }
        
        return resident;
    }
} 